package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.orewar.OreWarRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OreWarRecordDaoImpl extends TempDao {

    public Map<Long, List<OreWarRecord>> getOreWarRecordMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<OreWarRecord>> dataMap = new ConcurrentHashMap<>();
        String sql = "SELECT * FROM t_u_activity_orewar_record where activityId = ? order by recordTime desc";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                OreWarRecord data = initOreWarRecord(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(), new ArrayList<>());
                }
                dataMap.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean addOreWarRecordList(List<OreWarRecord> oreWarRecordList) {
        boolean result = false;
        String sql = "INSERT INTO t_u_activity_orewar_record(recordUid,userId,activityId,pitId,enemyUserId,enemyBaseData,recordTime,isWin) VALUES(?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (OreWarRecord info : oreWarRecordList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, info.getRecordUid()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getPitId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getEnemyUserId()));
            para.put(i++, new DbParameter(Types.VARCHAR, info.getEnemyBaseDataJsonStr()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getRecordTime()));
            para.put(i++, new DbParameter(Types.BOOLEAN, info.isWin()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    private OreWarRecord initOreWarRecord(ResultSet rs) throws SQLException {
        OreWarRecord data = new OreWarRecord();
        data.setRecordUid(rs.getString("recordUid"));
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setPitId(rs.getInt("pitId"));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        String baseUserInfoStr = rs.getString("enemyBaseData");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setEnemyBaseData(userBaseInfo);
        }
        data.setRecordTime(rs.getLong("recordTime"));
        data.setWin(rs.getBoolean("isWin"));
        return data;
    }
}
